**********************************
* Created by: Jason Premo
* Date: 9/7/2016
*
* Reviewed by:
* Date:
*
* Description: Creates the cash identity histograms and results. An older
* version of this file, written by David Zhange and using the old datasets,
* is stored in the archive.
*
* Filepath: /shared/sdsjxs/Townsend/scripts/cash_identity.do
*
********************************

clear all
set matsize 11000
set more off

local in_path /shared/sdsjxs/DCPC/Current_datasets/2012

*****

use "`in_path'/dcpc_2012_v2.dta", clear

sort prim_key date tran

*Create running cash balance
*expenditures
gen cash_tran = -amnt if pi == 1
*Withdrawals, conversions to
replace cash_tran = amnt if inlist(type, 4, 8, 10) | (type == 3 & how_reimbursed == 1) | (type == 6 & coin2cash_reimburse == 1)
*Deposits, conversions from
replace cash_tran = -amnt if inlist(type, 5, 7, 9, 11, 12)
drop if cash_tran == 0
gen error_bound = !missing(cash_tran)

rename start_csh_bal start_cash_bal
rename end_csh_bal end_cash_bal

*replace end_cash_bal = end_cash_bal/2

*Day level
collapse (sum) cash_tran error_bound (mean) start_cash_bal end_cash_bal, by(prim_key diary_day)

gen implied_end_cash = start_cash_bal+cash_tran
gen cash_error_day = implied_end_cash - end_cash_bal
gen goodcashid_day = abs(cash_error_day) <= error_bound

tab goodcashid_day

*Individual level
preserve

gen final_cash_bal = end_cash_bal if diary_day == 3
gen first_cash_bal = start_cash_bal if diary_day == 1

collapse (sum) cash_tran error_bound goodcashid_day (mean) final_cash_bal first_cash_bal, by(prim_key)

gen implied_end_cash = first_cash_bal+cash_tran
gen cash_error_all = implied_end_cash - final_cash_bal
gen goodcashid_all = abs(cash_error_all) <= error_bound

tab goodcashid_all
tab goodcashid_day

gen outbound_all = abs(cash_error_all) > error_bound & !missing(cash_error_all)
replace outbound_all = cash_error_all - error_bound if cash_error_all > 0 & outbound_all == 1
replace outbound_all = cash_error_all + error_bound if cash_error_all < 0 & outbound_all == 1

tempfile ind
save `ind', replace

restore 

*Merge in individual level errors
merge m:1 prim_key using `ind', nogen keepusing(goodcashid_all cash_error_all outbound_all)

*Only keep those on day one
bysort prim_key: replace goodcashid_all = . if _n != 1

gen outbound = abs(cash_error_day) > error_bound & !missing(cash_error_day)
replace outbound = cash_error_day - error_bound if cash_error_day > 0 & outbound == 1
replace outbound = cash_error_day + error_bound if cash_error_day < 0 & outbound == 1

gen prim_key2 = subinstr(prim_key, ":", "", .)
destring prim_key2, replace
xtset prim_key2 diary_day
gen dir_cashid = 1 if outbound > 0 & !missing(outbound)
replace dir_cashid = -1 if outbound < 0
replace dir_cashid = 0 if outbound == 0
xttrans dir_cashid

replace outbound = 10 if outbound > 10 & !missing(outbound)
replace outbound = -10 if outbound < -10
replace outbound_all = 10 if outbound_all > 10 & !missing(outbound)
replace outbound_all = -10 if outbound_all < -10

twoway (histogram outbound, percent start(-10.5) width(1) xtitle("Currency error ($)") color(emidblue)) (histogram outbound_all, percent start(-10.5) width(1) xtitle("Currency error ($)") fcolor(none) lcolor(black)), legend(ring(0) position(1) bmargin(large) label(1 "One Day") label(2 "Three Day") rows(2)) title("Distribution of Currency Errors") graphregion(color(white))
graph export "/shared/sdsjxs/Townsend/graphics/cash_id_histogram.pdf", as(pdf) replace
graph export "/shared/sdsjxs/Townsend/graphics/cash_id_histogram.png", as(png) replace
